package com.tracy.lifeproject.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.tracy.lifeproject.R;
import com.tracy.lifeproject.bean.SpecialInnerBean;
import com.tracy.lifeproject.bean.SummerBean;
import com.tracy.lifeproject.constant.UrlString;
import com.tracy.lifeproject.utils.CommonAdapter;
import com.tracy.lifeproject.utils.MyHolder;
import com.tracy.lifeproject.utils.OKHttpUtils;
import com.tracy.lifeproject.utils.PicassoRoundTransform;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by asus on 2016/7/24.
 */
public class Summer2Activity extends AppCompatActivity {
    @InjectView(R.id.summer_toolbar)
    Toolbar summerToolbar;
    @InjectView(R.id.summer_recycler)
    RecyclerView summerRecycler;
    @InjectView(R.id.img_sum_back)
    ImageView imgSumBack;
    @InjectView(R.id.text_sum_toolbar)
    TextView textSumToolbar;
    @InjectView(R.id.img_sum_share)
    ImageView imgSumShare;
    @InjectView(R.id.btn_sum_back)
    TextView btnSumBack;

    private Context mContext = this;
    private CommonAdapter adapter;
    private List<SpecialInnerBean.DataBean.ItemBean> mDatas;
    private String mSummerUrl;
    private String mName;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_summer);
        ButterKnife.inject(this);

        Intent intent = getIntent();
        mSummerUrl = UrlString.getSummerUrl2(intent.getIntExtra("id", 0));

        mName = intent.getStringExtra("name");

        textSumToolbar.setText(mName);

        Log.i("1234","网址----》"+mSummerUrl);
        /**
         * toolbar
         */
        setSupportActionBar(summerToolbar);

        /**
         * 适配器
         */
        initData();
        adapter = new CommonAdapter<SpecialInnerBean.DataBean.ItemBean>(this, mDatas, R.layout.item_summer) {
            @Override
            public void bindData(MyHolder holder, int position, SpecialInnerBean.DataBean.ItemBean bean) {
                /**
                 * 点赞数
                 */
                TextView tv = holder.getViewById(R.id.text_sum_good);
                tv.setText(mDatas.get(position).likes_count);
//                holder.setText(Integer.toString(mDatas.get(position).getLikes_count()),R.id.text_sum_good);
                /**
                 * 设置图片
                 */
                ImageView iv = holder.getViewById(R.id.img_sum);
                Picasso.with(mContext).load(mDatas.get(position).cover_image_url)
                        .transform(new PicassoRoundTransform())
                        .into(iv);
                /**
                 * 点击recycler单项跳转
                 */
                String str = mDatas.get(position).id;
                final int ID = Integer.parseInt(str) ;
                iv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(Summer2Activity.this, DetailActivity.class);
                        intent.putExtra("id", ID);
                        startActivity(intent);
                    }
                });
            }
        };
        summerRecycler.setAdapter(adapter);
        summerRecycler.setLayoutManager(new LinearLayoutManager(this));

    }

    /**
     * 解析数据
     */
    private void initData() {
        mDatas = new ArrayList<>();
        OKHttpUtils.newInstance(this).getAsyncMainData(mSummerUrl, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {
                Toast.makeText(Summer2Activity.this, "下载失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String result) {
                SpecialInnerBean specialInnerBean = new Gson().fromJson(result, SpecialInnerBean.class);
                List<SpecialInnerBean.DataBean.ItemBean> list = specialInnerBean.data.items;
                mDatas.addAll(list);
                adapter.notifyDataSetChanged();
            }
        }, 123);
    }

    /**
     * 点击返回
     */
    @OnClick({R.id.img_sum_back, R.id.btn_sum_back, R.id.img_sum_share})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_sum_back:
                finish();
                break;
            case R.id.btn_sum_back:
                finish();
                break;
            case R.id.img_sum_share:

                break;
        }
    }
}
